package ModelLayer;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * DVDCon functions as a container for DVD objects. It's set up as a singleton, to avoid the creation of several DVDCon objects.
 *
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class DVDCon
{   
    private HashMap<Integer, DVD> dvdCon;
    private static DVDCon instance;
    /**
     * Constructs one, and is limited to one construction of class DVDCon, taking no parameters but initializing dvdCon collection.
     */
    private DVDCon()
    {
        dvdCon = new HashMap<Integer, DVD>();
    }
    
    /**
     * creates a singleton instance which through its constructor creates the singleton collection.
     *
     * @return  instance    Returns the singleton instance to the caller.
     */
    public static DVDCon getInstance()
    {
        if(instance == null) {
            instance = new DVDCon();
        }
        return instance;
    }
    
    /**
     * Adds a DVD object to the singleton collection. Uses DVD ID and DVD object as key and value respectively.
     *
     * @param   d   An instance of class DVD.
     */
    public void addDVD(DVD d)
    {
        dvdCon.put(d.getID(), d);
    }
    
    /**
     * Finds a DVD object in the singleton collection by ID key.
     *
     * @param   id          The mapping key, used for finding a DVD object value mapped to this key specifically.
     * @return  DVD         Returns a DVD object mapped to the key value supplied as a parameter.
     * @throw   Exception   Throws an exception caught by the UI if no DVD object is mapped to the key.
     */
    public DVD getDVDID(int id) 
    {
        if(dvdCon.get(id) != null) {
            return dvdCon.get(id);
        }
        else {
            throw new IllegalArgumentException("DVD not found");
        }
        
    }

    /**
     * Finds an instance of DVD in the singleton collection by looking at the values in the HashMap.
     *
     * @param   title       Parameter for equating DVD title with supplied title.
     * @return  DVD         Returns an instance of DVD that matches the title supplied.
     * @throw   Exception   If no DVD contains the title supplied.
     */
    public DVD getDVDTitle(String title) {

        DVD tempDVD = null;
        for (DVD dvd : dvdCon.values()) {
            if (dvd.getTitle().equals(title)) {
              tempDVD = dvd;
            }
        }
        if(tempDVD != null) {
            return tempDVD;
        }
        else {
            throw new IllegalArgumentException("DVD not found");
        }
       
    }
        
    /**
     * Removes a DVD object from the singleton collection, identifying it by the key value supplied as a parameter.
     *
     * @param   id          The key value used to locate the mapping of a DVD object.
     * @return  true        If the id matched a key in the collection.
     * @throw   Exception   If dvdCon does not contain the supplied key.
     */
    public boolean deleteDVD(int id) 
    {
        if(dvdCon.containsKey(id)) {
            dvdCon.remove(id);
            return true;
        }
        else {
            throw new IllegalArgumentException("DVD not found");
        }
    }
    
    /**
     * Returns a list of all DVD objects in the singleton collection.
     *
     * @return  ArrayList   Returns a list view of all DVD objects in the HashMap.
     */
    public ArrayList<DVD> listAll()
    {
        ArrayList<DVD> dvds = new ArrayList<DVD>();
        for(DVD d : dvdCon.values()) {
            dvds.add(d);
        }
        return dvds;
    }
    
    /**
     * Returns the HashMap collection to the caller.
     *
     * @return  HashMap Returns the singleton HashMap collection to the caller.
     */
    public HashMap<Integer, DVD> getMap()
    {
        return dvdCon;
    }

}